package com.dabai.applicationmanager.transfer;

import android.Manifest;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.NetworkInfo;
import android.net.wifi.p2p.WifiP2pManager;
import android.util.Log;
import android.widget.Toast;

import androidx.core.app.ActivityCompat;

/**
 * Created by shuangyue on 2016/9/21 10:51.
 */
public class WifiP2pBroadcastReceiver extends BroadcastReceiver {

    private Context mContext;
    private WifiP2pManager mWifiP2pManager;
    private WifiP2pManager.Channel mChannel;
    private WifiP2pManager.PeerListListener mPeerListListener;
    private WifiP2pManager.ConnectionInfoListener mConnectionInfoListener;

    public WifiP2pBroadcastReceiver(Context mContext, WifiP2pManager mWifiP2pManager, WifiP2pManager.Channel mChannel,
                                    WifiP2pManager.PeerListListener mPeerListListener,
                                    WifiP2pManager.ConnectionInfoListener mConnectionInfoListener) {
        this.mContext = mContext;
        this.mWifiP2pManager = mWifiP2pManager;
        this.mChannel = mChannel;
        this.mPeerListListener = mPeerListListener;
        this.mConnectionInfoListener = mConnectionInfoListener;
    }

    @Override
    public void onReceive(Context context, Intent intent) {
        String action = intent.getAction();
        if (WifiP2pManager.WIFI_P2P_STATE_CHANGED_ACTION.equals(action)) {
            int wifiState = intent.getIntExtra(WifiP2pManager.EXTRA_WIFI_STATE, -1);
            if (WifiP2pManager.WIFI_P2P_STATE_DISABLED == wifiState) {
                Log.d("DBTAG", "onReceive: WIFI_P2P_STATE_DISABLED");
            } else if (WifiP2pManager.WIFI_P2P_STATE_ENABLED == wifiState) {
                Log.d("DBTAG", "onReceive: WIFI_P2P_STATE_ENABLED");
            }
        } else if (WifiP2pManager.WIFI_P2P_DISCOVERY_CHANGED_ACTION.equals(action)) {
            int dicoverState = intent.getIntExtra(WifiP2pManager.EXTRA_DISCOVERY_STATE, -1);
            if (WifiP2pManager.WIFI_P2P_DISCOVERY_STARTED == dicoverState) {
                Log.d("DBTAG", "onReceive: 开始搜索");
            } else if (WifiP2pManager.WIFI_P2P_DISCOVERY_STOPPED == dicoverState) {
                Log.d("DBTAG", "onReceive: 停止搜索");
            }
        } else if (WifiP2pManager.WIFI_P2P_PEERS_CHANGED_ACTION.equals(action)) {
            if (mWifiP2pManager != null) {
                if (ActivityCompat.checkSelfPermission(context, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
                       return;
                }
                mWifiP2pManager.requestPeers(mChannel, mPeerListListener);
            }
        }else if(WifiP2pManager.WIFI_P2P_CONNECTION_CHANGED_ACTION.equals (action)){
 
            NetworkInfo mNetworkInfo = intent.getParcelableExtra (WifiP2pManager.EXTRA_NETWORK_INFO);
            if(mNetworkInfo.isConnected ()){
                toast ("已经连接");
                if(mWifiP2pManager!=null){
                    mWifiP2pManager.requestConnectionInfo (mChannel,mConnectionInfoListener);
                }
            }else{
                toast ("连接断开");
            }
        }else if(WifiP2pManager.WIFI_P2P_THIS_DEVICE_CHANGED_ACTION.equals (action)){
            Log.d ("DBTAG", "onReceive: WIFI_P2P_THIS_DEVICE_CHANGED_ACTION");
        }
    }
    private void toast(String text){
        Toast.makeText (mContext,text,Toast.LENGTH_SHORT).show ();
    }
}